#include "llt_v6tov4.hh"

using namespace userFunction;

void
v6tov4::processData(lee::PktData * const pkt)
{
  tt::SixPacket * v6pkt;
  v6pkt = static_cast<tt::SixPacket *>(pkt);
  tt::Packet * v4pkt = new tt::Packet;

  // version and internet header length

  v4pkt->ipHdr.version = IPVERSION;
  v4pkt->ipHdr.ihl = 5;

  // type of service copied from traffic class

  v4pkt->ipHdr.tos = v6pkt->ipHdr.ip6_flow & 0x0FF00000;

  // total length copied from payload length, then add header

  v4pkt->ipHdr.tot_len = v6pkt->ipHdr.ip6_plen + 20;

  // id is all zero

  v4pkt->ipHdr.id = 0;

  // set more fragments to zero and don't fragment to one
  // set offset to zero

  v4pkt->ipHdr.frag_off = 0x4000;

  // ttl copied from hop count

  v4pkt->ipHdr.ttl = v6pkt->ipHdr.ip6_hlim;

  // protocol copies from next header

  v4pkt->ipHdr.protocol = v6pkt->ipHdr.ip6_nxt;

  // put the address of this machine into the source

  v4pkt->ipHdr.saddr = htonl(0xc0a80101);

  // the destination is a v4 inside a v6, so copy the lower bits

  v4pkt->ipHdr.daddr = v6pkt->ipHdr.ip6_dst.s6_addr32[0];

  normal(static_cast<lee::PktData *>(v4pkt));
  drop(pkt);
}
